# -*- coding:utf-8 -*-
import scrapy
import urllib
from scrapy.spiders import Spider,Rule
from scrapy.linkextractors import LinkExtractor
from scrapy.http import Request


class Jian1Spider(Spider):
	name = "jian1"
	allowed_domains = ["j1.com"]
	start_urls = [
		"http://www.j1.com/product/47109-61823.html#from_tj_cuxiao02"
	]


	def parse(self,response):

		item = {}
		productdetailfather = response.xpath('//div[@class="productarea-right"]')
		print '-------------------'
		if len(productdetailfather)>0:
			for pro in productdetailfather:
				#得到商品名称
				productname = productdetailfather.xpath('./div[@class="productarea-right-1"]/h1/text()').extract()
				#print len(productname)
				if len(productname)>1:
					print 'productname='+productname[1]
					item['productname'] = productname[1]
				else:
					print 'productname='+productname[0]
					item['productname'] = productname[0]
				#
				productdetails = productdetailfather.xpath('./div[@class="clearfix ptb10"]/div[@class="fl"]/div[@class="new_detail_top"]')
				#print productdetails
				#print len(productdetails)
				for productdetail in productdetails:
					#得到商品编号
					pronumdl = productdetail.xpath('./dl')[0]
					#print pronumdl
					productnumber = pronumdl.xpath('./dd/text()').extract()[0]
					print 'productnumber='+str(productnumber)
					item['productnumber'] = productnumber
					#得到参考价
					prorepricepath = productdetail.xpath('./input[@id="marketPrice"]')
					proreprice = prorepricepath.xpath('@value')[0].extract()
					item['referenceprice'] = proreprice
					print proreprice
					#得到当前售价
					propricepath = productdetail.xpath('./dl[@id="ecPriceDl"]/dd/span/strong/input[@id="ecPrice"]')
					proprice = propricepath.xpath('@value')[0].extract()
					item['realprice'] = proprice
					print proprice
					#得到商品规格
					prostandardpath = productdetail.xpath('./dl')[4]
					prostandard = prostandardpath.xpath('./dd/text()').extract()[0]
					print prostandard.encode('gbk')
					item['productstandard'] = prostandard.encode('gbk')
					#得到生产厂家
					profactorypath = productdetail.xpath('./dl')[5]
					profactory = profactorypath.xpath('./dd/text()').extract()[0]
					print profactory.encode('gbk')
					item['productfactory'] = profactory.encode('gbk')

		
		#得到累计销量
		leijixiaoliangpath =  response.xpath('//iframe[@id="commentIframe"]')
		if len(leijixiaoliangpath)>0:
			leijixiaoliang = leijixiaoliangpath.xpath('@src').extract()[0]
			print '~~~~~~~~~~~~~~~~~~~~~~~'
			print 'leijixiaoliang='+leijixiaoliang
			yield Request(url=leijixiaoliang,meta={'item':item},callback=self.parse_report, dont_filter=True) 
			print '~~~~~~~~~~~~~~~~~~~~~'
				

	def parse_report(self,response):
		print '********************'
		print "PARSE ITEMS. Visiting %s" % response.url
		item = response.meta['item']
		productdetailfather = response.xpath('//table[@class="table_order_list"]/tr')
		for pro in productdetailfather:
			protd = pro.xpath('./td/text()')
			for protdnew in protd:
				print protdnew.extract().encode('gbk')
		#print len(item)
		return item

	
			
		